from apps.common.base.views import *
from apps.system.role.serializers import *
from apps.system.role.dao import RoleDao
from apps.system.role.filters import RoleFilter

class BaseRoleViewSet(BaseViewSet):
    serializer_class = RoleModelSerializer
    filter_class = RoleFilter
    DAO_CLASS = RoleDao

    @action(methods=['post'], detail=False)
    def saveAction(self, request):
        params = getRequestParams(self.request)
        obj_id = params.get('id')
        if obj_id:
            obj = RoleDao().findOneByObjId(obj_id)
            if obj.key != params.get('key'):
                objs = RoleDao().findAll().filter(key=params.get('key'))
                if objs.exists():
                    return Response(response_message('角色关键字已存在'))
            self.DAO_CLASS().update(obj_id, params)
        else:
            objs = RoleDao().findAll().filter(key=params.get('key'))
            if objs.exists():
                return Response(response_message('角色关键字已存在'))
            self.DAO_CLASS().create(params)
        return Response(response_success('保存成功'))

    